<?php
apf_require_class("APF_Interceptor");
apf_require_class("Util_Encrypt");
class AuthCheckInterceptor extends APF_Interceptor {
    const SESSION_ID_KEY = "sessid";

    public function before() {
        $request = APF::get_instance()->get_request();
        $this->decryptCookie($request);

        return self::STEP_CONTINUE;
    }

    protected function decryptCookie($request) {
        $cookiename = APF::get_instance()->get_config("AuthCookieName");
        $cookie = $request->get_cookie($cookiename);

        if ($cookie) {
            $key = APF::get_instance()->get_config("ecrypt_str");
            $cookiestr = Util_Encrypt::decrypt($cookie,$key);

            list($user_id,$user_code,$user_name,$user_email) = explode("\t",$cookiestr);

            $request->set_user_id($user_id);
            $request->set_user_name(base64_decode($user_name));
            $request->set_user_code($user_code);
            $request->set_user_email($user_email);
        }

    }

    public function after () {
        return self::STEP_CONTINUE;
    }
}